package com.asset.salesforce.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.asset.salesforce.model.Account;
import com.asset.salesforce.model.Lead;

@Repository
public class SearchDaoImpl {
	
	@Autowired
	private SessionFactory sessionFactory;
//	@SuppressWarnings({ "unchecked", "deprecation" })
//	public void index() {
//		Session session =  sessionFactory.getCurrentSession();
//		
//		FullTextSession fullTextSession = Search.createFullTextSession(session);
//		//Transaction tx = fullTextSession.beginTransaction();
//		List<Lead> leads = session.createQuery("from Lead as lead").list();
//		for (Lead lead : leads) {
//		fullTextSession.index(lead);
//		}
//		List<Account> accounts = session.createQuery("from Account as account").list();
//		for (Account account : accounts) {
//		fullTextSession.index(account);
//		}
//		List<Contact> contacts= session.createQuery("from Contact as contact").list();
//		for (Contact contact : contacts) {
//		fullTextSession.index(contact);
//		}
//		List<StockSku> skus = session.createQuery("from StockSku as sku").list();
//		for (StockSku sku : skus) {
//		fullTextSession.index(sku);
//		}
//		
//		fullTextSession.flushToIndexes();
//	}
	
	@SuppressWarnings("unchecked")
	public List<Lead> searchLead(String query) {
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(Lead.class);
		
		criteria.add(Restrictions.like("fName", "%" + query + "%"));
		criteria.add(Restrictions.like("lName", "%" + query + "%"));
		criteria.add(Restrictions.like("companyName", "%" + query + "%"));
		criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
		
		List<Lead> leads = new ArrayList<Lead>();
		leads = criteria.list();
		return leads;
	}
	
	@SuppressWarnings("unchecked")
	public List<Account> searchAccount(String query) {
		
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(Account.class);
		
		criteria.add(Restrictions.like("accountName", "%" + query + "%"));
		criteria.add(Restrictions.like("email", "%" + query + "%"));
		criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
		
		List<Account> accounts = new ArrayList<Account>();
		accounts = criteria.list();
		return accounts;
	}
	
//	@SuppressWarnings("unchecked")
//	public List<Contact> searchContact(String query) {
//		
//		Session session = sessionFactory.getCurrentSession();
//		FullTextSession fullTextSession = Search.getFullTextSession(session);
//		//String [] fields = new String[]{"phone","email","webSite","parentName","mobile","lName","fName"};
//		String [] fields = new String[]{"email","lName","fName"};
//		MultiFieldQueryParser multiFieldQueryParser =new MultiFieldQueryParser(fields, new StandardAnalyzer());
//		try {
//			query = query+"*";
//			org.apache.lucene.search.Query query2 = multiFieldQueryParser.parse(query);
//			org.hibernate.Query query3 = fullTextSession.createFullTextQuery(query2, Contact.class);
//			List<Contact> contacts = new ArrayList<Contact>();
//			 contacts = query3.list();
//			return  contacts;
//		} catch (ParseException e) {
//			e.printStackTrace();
//			return null;
//		}
//	}

}
